package org.xiaohanghu.starter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author xiaohanghu
 */
public class StartConfigFactory {

	public static Logger log = Logger.getLogger();

	/** *.jar资源 */
	public static final String PROPERTY_JAR_RESOURCES_NAME = "jar-resources";
	/** *.class资源 */
	public static final String PROPERTY_CLASSES_RESOURCES_NAME = "classes-resources";
	/** main函数类名 */
	public static final String PROPERTY_MAIN_CLASS_NAME = "main-class";
	/** JVM启动参数文件地址 */
	public static final String PROPERTY_JVM_OPTIONS_NAME = "jvm-options";
	/** 系统参数配置地址 */
	public static final String PROPERTY_SYSTEM_PROPERTIES_NAME = "system-properties";

	public static final String PROPERTY_LOG_LEVEL_NAME = "log-level";

	public static StartConfig createStartConfig(Map<String, String> config,
			URL home, URL currentUrl) throws MalformedURLException,
			URISyntaxException {

		StartConfig startConfig = new StartConfig(home, currentUrl);

		String mainClass = config.get(PROPERTY_MAIN_CLASS_NAME);
		startConfig.setMainClass(mainClass);
		String jarUrls = config.get(PROPERTY_JAR_RESOURCES_NAME);
		startConfig.addJarResources(jarUrls);
		String classesUrls = config.get(PROPERTY_CLASSES_RESOURCES_NAME);
		startConfig.addClassesResources(classesUrls);
		String jvmOptionPath = config.get(PROPERTY_JVM_OPTIONS_NAME);
		startConfig.addJvmOptions(jvmOptionPath);
		String systemPropertiesPath = config
				.get(PROPERTY_SYSTEM_PROPERTIES_NAME);
		startConfig.addSystemProperties(systemPropertiesPath);

		String logLevel = config.get(PROPERTY_LOG_LEVEL_NAME);
		startConfig.setLogLever(logLevel);

		return startConfig;
	}

	public static StartConfig createStartConfig(Properties configProperties,
			URL home, URL currentUrl) throws MalformedURLException,
			URISyntaxException {
		Map<String, String> config = new HashMap<String, String>();
		PropertiesUtils.addAllProperties(config, configProperties);
		return createStartConfig(config, home, currentUrl);
	}

	public static StartConfig createStartConfig(String configPath,
			String startHome) throws IOException, URISyntaxException {
		URL home = null;
		URL currentUrl = null;
		File file = new File(startHome);
		home = file.toURI().toURL();
		file = new File(configPath);
		currentUrl = file.getParentFile().toURI().toURL();

		Properties properties = null;
		try {
			properties = PropertiesUtils.loadProperties(configPath);
		} catch (FileNotFoundException e) {
			// e.printStackTrace();
			log.error("Can't find config file by path : " + configPath);
			throw e;
		} catch (IOException e) {
			log.error("Can't read config file : " + configPath);
			throw e;
		}
		StartConfig config = null;
		try {
			config = StartConfigFactory.createStartConfig(properties, home,
					currentUrl);
		} catch (MalformedURLException e) {
			log.error("Read config throws MalformedURLException!");
			throw e;
		} catch (URISyntaxException e) {
			log.error("Read config throws URISyntaxException!");
			throw e;
		}
		return config;
	}

}
